/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.pdf;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Unity;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.ipti.br.tag.bean.dataManipulation.WeightAndHeightAuxiliator;

/**
 *
 * @author FelipeMSX
 */
@Stateless
public class WeightAndHeightPdf extends AbstractPdf {
    
    public void run(List<WeightAndHeightAuxiliator> whAs, Unity logged, Unity selected, Campaign campaign)
            throws FileNotFoundException, DocumentException, IOException, Exception {
        
        this.init("LPA"+ logged.getName()+"-"+campaign.getName()+ "-"+selected.getName());
        
        generateScope(whAs, logged, selected, campaign);
        
        close();
        export();
    }
    
    public void generateScope(List<WeightAndHeightAuxiliator> whAs, Unity logged, Unity selected, Campaign campaign)
            throws Exception {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();            
        Image image;
        image = Image.getInstance(externalContext.getRealPath("SLDI.jpg"));
        image.scaleAbsoluteWidth(140);
        image.scaleAbsoluteHeight(65);
        image.setAlignment( Element.ALIGN_CENTER);	
        image.setSpacingAfter(25);
        Paragraph paragraphSpace = new Paragraph("",super.getFontTextBold());
        paragraphSpace.setSpacingAfter(7);
        Paragraph paragraph1 = new Paragraph("Colégio: "+ logged.getName() +".",super.getFontTextBold());	
        paragraph1.setAlignment( Element.ALIGN_JUSTIFIED);
        paragraph1.setSpacingAfter(4);

        Paragraph paragraph2 = new Paragraph("Campanha"+ campaign.getName() +".",super.getFontTextBold());	
        paragraph2.setAlignment( Element.ALIGN_JUSTIFIED);
        paragraph2.setSpacingAfter(4);

        Paragraph paragraph333 = new Paragraph("Turma: "+ selected.getName() +".",super.getFontTextBold());	
        paragraph333.setAlignment( Element.ALIGN_JUSTIFIED);
        paragraph333.setSpacingAfter(15);

        PdfPTable table = new PdfPTable(3);        
        table.setWidthPercentage(100);
        PdfPCell cellName = new PdfPCell(new Paragraph("Nome",super.getFontTextBold()));
        PdfPCell cellWeight = new PdfPCell(new Paragraph("Peso",super.getFontTextBold())); 
        PdfPCell cellHeight = new PdfPCell(new Paragraph("Altura",super.getFontTextBold()));
        table.addCell(cellName);
        table.addCell(cellWeight);
        table.addCell(cellHeight);

        if ( whAs.isEmpty() || whAs != null){
            for( WeightAndHeightAuxiliator element : whAs){
                cellName = new PdfPCell(new Paragraph( element.getHuman().getName(),super.getFontTextNORMAL()));
                cellWeight = new PdfPCell(new Paragraph("",super.getFontTextNORMAL()));
                cellHeight = new PdfPCell(new Paragraph("",super.getFontTextNORMAL()));                
                table.addCell(cellName);
                table.addCell(cellWeight);
                table.addCell(cellHeight);
            }
        }
        super.getDocument().add(image);
        super.getDocument().add( paragraphSpace );
        super.getDocument().add(paragraph1);
        super.getDocument().add(paragraph2);
        super.getDocument().add(paragraph333);
        super.getDocument().add(table);
    }
}
